var upload = function(configx){
    var config = {
        "btnSelector":"#coverupload",
        "imgSelector":".img",
        "hidSelector":".hid",
        "fileAccept":"image/png,image/jpeg",
        "uploadUrl":"/file/upload/article.html"
    }
    $.extend(config,configx);
    //添加事件
    var fileId = "upload_input_"+config['btnSelector'].replace("#","").replace("\.","");
    var fileStr = "<input type='file' style='display: none' accept='"+config['fileAccept']+"' id='"+fileId+"' />";
    $(config['btnSelector']).parent().append(fileStr);

    $("#"+fileId).on("change",function(){
        var loadingIndex = layer.load(1, {
            shade: [0.1,'#fff']
        });
        var file = this.files[0];
        if (!file){ layer.close(loadingIndex);return ; }
        var size = file.size,type=file.type,name=file.name;

        if(size > 1048576){
            layer.close(loadingIndex);
            layer.msg("请上传小于1M的图片！");
            return;
        }

        var fd = new FormData();
        fd.append("file", file);
        $.ajax({
            url:config['uploadUrl'],
            type:'post',
            data:fd,
            cache: false,
            processData: false,
            contentType: false,
            success:function(data){
                layer.close(loadingIndex);
                var msg = data.msg;
                if(data.code == 1){
                    layer.msg("上传成功!")
                    $(config['imgSelector']).attr("src",data.obj.src);
                    $(config['hidSelector']).val(data.obj.src);
                }else{
                    layer.msg(msg);
                }
            },
            error:function(){
                layer.close(loadingIndex);
                layer.msg("上传失败!")
            }
        })
    })

    $(config['btnSelector']).on("click",function(){
        $("#"+fileId).click();
    })
}

